#!/usr/bin/python
# -*- encoding: UTF-8 -*-

import xml.dom.minidom

from cairowidget.basewidget import *

class CairoGladeSimple:
    def __init__(self, xml):
        self.dom = None
        self.widgets = {}
        self.load(xml)
        
    def load(self,name):
        self.dom = xml.dom.minidom.parse(name)
        self.__parse_widgets()
        
    def __getattr__(self,attr):
        return getattr(self.dom,attr)

    def get_widget(self,id):
        try:
            return self.widgets[id]
        except:
            return None
    
    '''
    EVAL IS DANGEROUS ;)
    Fail @ Pythonism :P
    '''
    def __parse_widgets(self):
        stack = [] #trying to remove recursion, only to use one remotely
        widget = node = None
        w = None # Parent widget
        s = 0
        node = self#.childNodes[0] #.dom
        while True:
            
            #print "Parent:",node
            for i in range(s, len(node.childNodes)):
                e = node.childNodes[i]
                
                if e.localName == "widget" :#e.childNodes and e.nodeType == e.ELEMENT_NODE and 
                
                    widget = eval(e.getAttribute("class"))()
                    self.widgets[e.getAttribute("id")] = widget
                    if w:
                        self.widgets[w].add_child(widget)
                        #print "add_Child"
                    w = e.getAttribute("id")
                    #print "Widget", w
                    #Go deeper
                    stack.append((node,i+1)) #Save current node we are iterating
                    stack.append((e,0)) #Set next node to be iterated
                    break
                
                elif e.localName == "property":
                
                    #if e.getAttribute("type") == "method":
                    try:
                        if hasattr(self.widgets[w],"set_"+e.getAttribute("name")): #attempt at sanity check
                            eval( "self.widgets[w].set_%s(%s)" %(e.getAttribute("name"),e.childNodes[0].nodeValue))
                    except:
                        print "Could not set as widget.set_%s(%s)" %(e.getAttribute("name"),e.childNodes[0].nodeValue)
                    '''
                    elif e.getAttribute("type") == "attrib":
                        try:
                            if hasattr(self.widget[w],e.getAttribute("name")):
                                setattr( self.widgets[w], e.getAttribute("name"), e.nodeValue)
                        except :
                            print "Cannot set property",e.getAttribute("name"), e.childNodes[0].nodeValue
                    else:
                        print "Unknown property:",e.getAttribute("name")
                    '''
                    
                elif e.localName == "child" or e.localName=="interface":
                    #print "Child"
                    stack.append((node,i+1))
                    stack.append((e,0))
                    break
                        
            if not len(stack):
                break
            node,s = stack.pop()
            
            #print "Pop",node,s, depth
                    

if __name__== "__main__":
    p = CairoGladeSimple("test.xml")
    #p.load("test.xml")
    #print p.childNodes[0].localName, p.childNodes[0].childNodes[1].childNodes[1].childNodes[0]
    #print p.getElementsByTagName("widget")

    def value(w,e):
        print "Value",w.get_value()
        
    p.get_widget("cairowindow1").show_all()
    p.get_widget(u"volume").connect("value-changed",value)
    gtk.main()
